package StacktoQueue;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;

class MyQueue {

    private Stack<Integer> s1;
    private Stack<Integer> s2;
    public MyQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }
    
    public void push(int x) {
        s1.push(x);
    }
/*
    // 出队列
    public int pop() {
        while(!s1.empty()){
            //将 s1中的值全部倒叙给s2,s2的栈顶就是要出的元素
            s2.push(s1.pop());
        }
        int n=s2.pop();
        while(!s2.empty()){
            s1.push(s2.pop());
        }
        return n;
    }
    
    public int peek() {
        while(!s1.empty()){
            //将 s1中的值全部倒叙给s2,s2的栈顶就是要出的元素
            s2.push(s1.pop());
        }
        int n=s2.peek();
        while(!s2.empty()){
            s1.push(s2.pop());
        }
        return n;
    }
    */


    public int pop() {
        if (s2.isEmpty()) {
            in2out();
        }
        return s2.pop();
    }

    public int peek() {
        if (s2.isEmpty()) {
            in2out();
        }
        return s2.peek();
    }



    private void in2out() {
        while (!s1.isEmpty()) {
            s2.push(s1.pop());
        }
    }

    public boolean empty() {
        return s1.empty() && s2.empty();
    }
}